package com.macro.mall.controller;

import com.alibaba.fastjson.JSON;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.utils.HttpKit;
import com.macro.mall.utils.HttpUrlAndFile;
import com.macro.mall.utils.ImageZipUtil;
import com.macro.mall.vo.ImgVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
@Api(tags = "WxController", description = "微信")
@RequestMapping("/wx")
public class WxController {
    @Autowired
    private Environment env;

    public String accessToken() {
        RestTemplate restTemplate =new RestTemplate();
        String url = String.format(
                "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
                env.getProperty("wx.appid"), env.getProperty("wx.secret"));
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        String body = responseEntity.getBody();
        Map<String, Object> resMap = JSON.parseObject(body,Map.class);
        System.out.println(resMap);
        String token = resMap.get("access_token").toString();
        return token;
    }

    @ApiOperation("添加商品")
    @PostMapping("/add/product")
    @ResponseBody
    public CommonResult<String> addProduct(@RequestParam String coverImgUrl,
                                           @RequestParam String name,
                                           @RequestParam Integer priceType,
                                           @RequestParam float price,
                                           @RequestParam float price2,
                                           @RequestParam String  url){
        String urls = "https://api.weixin.qq.com/wxaapi/broadcast/goods/add?access_token="+accessToken();
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        Map<String,Object> map = new HashMap<>();
        map.put("coverImgUrl",coverImgUrl);
        map.put("name",name);
        map.put("priceType",priceType);
        map.put("price",price);
        map.put("price2",price2);
        map.put("url",url);
        Map<String,Object> map1 = new HashMap<>();
        map1.put("goodsInfo",map);
        String data = JSONObject.fromObject(map1).toString();
        String res1 = HttpKit.post(urls,data,headers);
        return CommonResult.success(res1);
    }
    @ApiOperation("添加商品图片")
    @PostMapping("/add/img")
    @ResponseBody
    public CommonResult<String> addImg(@RequestBody ImgVO imgVO) {
        String urls = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token="+accessToken()+"&type=image";
        System.out.println(imgVO.getImg());
        RestTemplate rest = new RestTemplate();

        File  urlSFile=HttpUrlAndFile.getFile(imgVO.getImg(),"3433.jpg");
        //File urlSFile=new File(urlS);
        File url=ImageZipUtil.zipImageFile(urlSFile, 300, 300, 3f);
        FileSystemResource resource = new FileSystemResource(url);
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("media", resource); //MultipartFile的名称
        String rs = rest.postForObject(urls, param, String.class);
        urlSFile.deleteOnExit();
        return CommonResult.success(rs);
    }
    @ApiOperation("商品列表")
    @GetMapping("/list/product")
    @ResponseBody
    public CommonResult<String> productList(@RequestParam Integer offset,@RequestParam Integer limit,@RequestParam Integer status){
        String urls = "https://api.weixin.qq.com/wxaapi/broadcast/goods/getapproved?access_token="+accessToken();
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        Map<String,String> map = new HashMap<>();
        map.put("offset",offset.toString());
        map.put("limit",limit.toString());
        map.put("status",status.toString());
        String data = JSONObject.fromObject(map).toString();
        String aa= HttpKit.get(urls,map,headers);
        return CommonResult.success(aa);
    }
    @ApiOperation("删除商品")
    @PostMapping("/del/product")
    @ResponseBody
    public CommonResult<String> productWithdraw(@RequestParam Integer goodsId){
        String urls = "https://api.weixin.qq.com/wxaapi/broadcast/goods/delete?access_token="+accessToken();
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        Map<String,String> map = new HashMap<>();
        map.put("goodsId",goodsId.toString());
        String data = JSONObject.fromObject(map).toString();
        return CommonResult.success( HttpKit.post(urls,data,headers));
    }
}
